package model;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class WarArtifact extends JavaArtifact {

	private static final String WEB_INF = "WEB-INF";
	private static final String LIB = WEB_INF + "/lib";
	private static final String CLASSES = WEB_INF + "/classes";

	private List<JarArtifact> jarArtifacts;
	private List<ClassArtifact> classArtifacts;

	protected WarArtifact(String path) {
		super(path, JavaFileExtension.WAR);
		jarArtifacts = new LinkedList<JarArtifact>();
		classArtifacts = new LinkedList<ClassArtifact>();
	}

	@Override
	public ClassVersion calculateClassVersion() {
		JarInputStream jarInputStream = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(getPath());
			jarInputStream = new JarInputStream(fileInputStream);

			JarEntry jarEntry = null;
			while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
				String jarEntryPath = jarEntry.getName();
				if (JavaFileExtension.hasJavaFileExtension(jarEntryPath)) {
					if (isInClasses(jarEntryPath)) {
						ClassArtifact classArtifact = (ClassArtifact) JavaArtifactFactory.create(jarEntryPath);
						classArtifact.calculateClassVersion(jarInputStream);
						classArtifacts.add(classArtifact);
						setClassVersion(classArtifact.getClassVersion());
					}
					else if(isInLib(jarEntryPath)) {
						JarArtifact jarArtifact = new JarArtifact(jarEntryPath);
						jarArtifact.calculateClassVersion(jarInputStream);
						jarArtifacts.add(jarArtifact);
					}
				}
			}

//			ClassVersion classVersion = ClassArtifact
//					.getClassVersion(jarInputStream);
//			super.setVersion(classVersion.getMajor(), classVersion.getMinor());

			return getClassVersion(); 
		} catch (IOException e) {
			throw new InvalidJavaArtifactContentException(
					"IO exception occured: " + e.getMessage());
		} finally {
			try {
				if (jarInputStream != null) {
					jarInputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private boolean isInClasses(String jarEntryName) {
		
		return jarEntryName.startsWith(CLASSES);
	}

	private boolean isInLib(String jarEntryName) {
		
		return jarEntryName.startsWith(LIB);
	}
	
	public List<ClassArtifact> getClassArtifacts() {
		return classArtifacts;
	}
	
	public List<JarArtifact> getJarArtifacts() {
		return jarArtifacts;
	}

	@Override
	public ClassVersion calculateClassVersion(InputStream inputStream) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String toString() {
		String superToString = super.toString();
		superToString += "\r\n#CLASSES#\r\n";
		for (ClassArtifact classArtifact : this.classArtifacts) {
			superToString += classArtifact.toString() + "\r\n";
		}
		
		superToString += "\r\n#JARS#\r\n";
		for (JarArtifact jarArtifact : this.jarArtifacts) {
			superToString += jarArtifact.getPath() + ":";
			superToString += jarArtifact.getClassVersion() + "\r\n";
		}
		
		return superToString;
	}
	
	

//	public static void main(String[] args) {
//		WarArtifact artifact = new WarArtifact("D:\\Documents\\Kristal\\FT 234033\\prod fail\\kristal.war");
//		artifact.calculateClassVersion();
//		System.out.println(artifact);
//		List<ClassArtifact> classArtifacts = artifact.getClassArtifacts();
//		for (ClassArtifact classArtifact : classArtifacts) {
//			System.out.println(classArtifact);
//		}
//		
//		List<JarArtifact> jarArtifacts = artifact.getJarArtifacts();
//		for (JarArtifact jarArtifact : jarArtifacts) {
//			System.out.println(jarArtifact);
//		}
//	}

}
